package com.nari.springboot.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.nari.springboot.entity.JsonResult;
import com.nari.springboot.entity.User;
import com.nari.springboot.service.UserService;

@Controller
public class UserController {
 
	@Autowired
	private UserService userService;
	
	@RequestMapping("/")
	public String init() {
		//在fixbug分支修改完了bug 
		return "userList";
	}
 
	/**
	 * 根据ID查询用户
	 * @param id
	 * @return
	 */
	// 2018/11/12用GetMapping 替换RequestMapping  @RequestMapping(value = "user/{id}", method = RequestMethod.GET) 下面类似
	@GetMapping(value = "user/{id}")// 2018/11/12用GetMapping 替换RequestMapping  @RequestMapping(value = "user/{id}", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") int id){
		JsonResult r = new JsonResult();
		try {
			User user = userService.getUserById(id);
			r.setResult(user);
			r.setStatus("ok");
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");
			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
 
	/**
	 * 查询用户列表
	 * @return
	 */
	//@ResponseBody
	@GetMapping(value = "users")
	public String getUserList (Model model){
//		JsonResult r = new JsonResult();
//		try {
		List<User> userList = userService.getUserList();
		model.addAttribute("userList", userList);
	    //ModelAndView mav = new ModelAndView("userList");
	    //mav.addObject("userList", userList);
			//model.addAttribute("users", users);
//			r.setResult(users);
//			r.setStatus("ok");
//		} catch (Exception e) {
//			r.setResult(e.getClass().getName() + ":" + e.getMessage());
//			r.setStatus("error");
//			e.printStackTrace();
//		}
		return "userList";
	}
 
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
/*	  @PostMapping(value = "user")
	  @RequestMapping(value = "user", method = RequestMethod.POST)*/
	@PostMapping(value = "user")
	public ResponseEntity<JsonResult> add (@RequestBody User user){
		System.out.println("id=" + user.getId() + "userName=" + user.getUserName() + "passWord=" + user.getPassWord());
		JsonResult r = new JsonResult();
		try {
			int orderId = userService.add(user);
			if (orderId < 0) {
				r.setResult(orderId);
				r.setStatus("fail");
			} else {
				r.setResult(orderId);
				r.setStatus("ok");
			}
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");
 
			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
 
	/**
	 * 根据id删除用户
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "user/{id}")
	public ResponseEntity<JsonResult> delete (@PathVariable(value = "id") int id){
		JsonResult r = new JsonResult();
		try {
			int ret = userService.delete(id);
			if (ret < 0) {
				r.setResult(ret);
				r.setStatus("fail");
			} else {
				r.setResult(ret);
				r.setStatus("ok");
			}
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");
 
			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
 
	/**
	 * 根据id修改用户信息
	 * @param user
	 * @return
	 */
	@PutMapping(value = "user/{id}")
	public ResponseEntity<JsonResult> update (@PathVariable("id") int id, @RequestBody User user){
		JsonResult r = new JsonResult();
		try {
			int ret = userService.update(id, user);
			if (ret < 0) {
				r.setResult(ret);
				r.setStatus("fail");
			} else {
				r.setResult(ret);
				r.setStatus("ok");
			}
		} catch (Exception e) {
			r.setResult(e.getClass().getName() + ":" + e.getMessage());
			r.setStatus("error");
 
			e.printStackTrace();
		}
		return ResponseEntity.ok(r);
	}
 
}
